const util = require('../../utils/util.js');

const api = require('../../config/api.js');

const user = require('../../services/user.js'); //获取应用实例


const app = getApp();
Page({
  data: {
    floorGoods: [],
    openAttr: false,
    showChannel: 0,
    showBanner: 0,
    showBannerImg: 0,
    banner: [],
    index_banner_img: 0,
    userInfo: {},
    imgurl: '',
    sysHeight: 0,
    loading: 0,
    autoplay: true,
    showContact: 1
  },
  onPageScroll: function (e) {
    let scrollTop = e.scrollTop;
    let that = this;

    if (scrollTop >= 2000) {
      that.setData({
        showContact: 0
      });
    } else {
      that.setData({
        showContact: 1
      });
    }
  },
  onHide: function () {
    this.setData({
      autoplay: false
    });
  },
  goSearch: function () {
    tt.navigateTo({
      url: '/pages/search/search'
    });
  },
  goCategory: function (e) {
    let id = e.currentTarget.dataset.cateid;
    tt.setStorageSync('categoryId', id);
    tt.switchTab({
      url: '/pages/category/index'
    });
  },
  handleTap: function (event) {//阻止冒泡 
  },
  onShareAppMessage: function () {
    let info = tt.getStorageSync('userInfo');
    return {
      title: '海风小店',
      desc: '开源微信小程序商城',
      path: '/pages/index/index?id=' + info.id
    };
  },
  toDetailsTap: function () {
    tt.navigateTo({
      url: '/pages/goods-details/index'
    });
  },
  getIndexData: function () {
    let that = this;
    util.request(api.IndexUrl).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          floorGoods: res.data.categoryList,
          banner: res.data.banner,
          channel: res.data.channel,
          notice: res.data.notice,
          loading: 1
        });
        let cartGoodsCount = '';

        if (res.data.cartCount == 0) {
          tt.removeTabBarBadge({
            index: 2
          });
        } else {
          cartGoodsCount = res.data.cartCount + '';
          tt.setTabBarBadge({
            index: 2,
            text: cartGoodsCount
          });
        }
      }
    });
  },
  onLoad: function (options) {
    this.getChannelShowInfo();
  },
  onShow: function () {
    this.getIndexData();
    var that = this;
    let userInfo = tt.getStorageSync('userInfo');

    if (userInfo != '') {
      that.setData({
        userInfo: userInfo
      });
    }

    ;
    let info = tt.getSystemInfoSync();
    let sysHeight = info.windowHeight - 100;
    this.setData({
      sysHeight: sysHeight,
      autoplay: true
    });
    tt.removeStorageSync('categoryId');
  },
  getChannelShowInfo: function (e) {
    let that = this;
    util.request(api.ShowSettings).then(function (res) {
      if (res.errno === 0) {
        let show_channel = res.data.channel;
        let show_banner = res.data.banner;
        let show_notice = res.data.notice;
        let index_banner_img = res.data.index_banner_img;
        that.setData({
          show_channel: show_channel,
          show_banner: show_banner,
          show_notice: show_notice,
          index_banner_img: index_banner_img
        });
      }
    });
  },
  onPullDownRefresh: function () {
    tt.showNavigationBarLoading();
    this.getIndexData();
    this.getChannelShowInfo();
    tt.hideNavigationBarLoading(); //完成停止加载

    tt.stopPullDownRefresh(); //停止下拉刷新
  }
});